package com.xuan.juc.homework;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class HomeWork2 {


    public static void main(String[] args) {
        T t = new T();
        new Thread(t,"A").start();
        new Thread(t,"B").start();

    }




}


class T implements Runnable{
    public volatile int total = 10000;
    Lock lock = new ReentrantLock();
    @Override
    public void run() {

        while (true){
            lock.lock();
            try {
                if (total<=0){
                    System.out.println("余额不足");
                    break;
                }else{
                    total = total - 1000;
                    System.out.println(Thread.currentThread().getName() + "获取了1000元，余额为："+ total);
                }
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}




